import { AppDataSource } from "@/config/database";
import { Customer } from "@/entities/basis/customer.entity";
import { ErrorHandler } from "@/middlewares/error.middleware";
import { Context } from "koa";

/** 删除客户 */
export async function deleteCustomer(ctx: Context) {
    const { id } = ctx.params;

    if (!id) {
        throw new ErrorHandler('客户ID不能为空', 400);
    }

    const customerRepository = AppDataSource.getRepository(Customer);
    
    // 检查客户是否存在
    const customer = await customerRepository.findOne({
        where: { id: id as any }
    });

    if (!customer) {
        throw new ErrorHandler('客户不存在', 404);
    }

    // 删除客户
    await customerRepository.remove(customer);

    ctx.body = {
        message: '客户删除成功',
        id: Number(id)
    };
} 